<template>
  <el-dialog
    :visible.sync="visible"
    :title="!dataForm.id ? $t('add') : $t('update')"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form
      :model="dataForm"
      ref="dataForm"
      :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'"
    >
      <el-form-item
        label="区县"
        prop="name"
      >
        <span v-if="dataForm.id">
          {{$Dictionaries("CityRegion", dataForm.id)}}
        </span>

         <el-select
         v-else
            v-model="dataForm.id"
            placeholder="请选择"
            clearable
          >
            <el-option
              v-for="item in this.$store.state.CityRegion"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>

      </el-form-item>
      <el-form-item
        label="商圈"
        prop="districtCode"
      >
        <el-tag
          :key="index"
          v-for="(item,index) in dataForm.list"
          size='medium'
          closable
          :disable-transitions="true"
          @close="handleClose(item)"
          @click="handleClick(item)"
          style="margin-right:10px;"
        >
          {{item.name}}
        </el-tag>
        <el-input
          class="input-new-tag"
          v-if="inputVisible"
          v-model="inputValue"
          ref="saveTagInput"
          size='mini'
          @keyup.enter.native="handleInputConfirm"
          @blur="handleInputConfirm"
          style="width:80px"
        >
        </el-input>
        <el-button
          v-else
          class="button-new-tag"
          @click="showInput"
          size='mini'
          style="width:80px"
        >添加</el-button>

      </el-form-item>
      <!-- <el-form-item
        label="经纬度"
        prop="latitude"
      >
        <el-input
          v-model="dataForm.latitude"
          placeholder="经纬度"
        ></el-input>
      </el-form-item> -->
    </el-form>
    <!-- <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button
        type="primary"
        @click="dataFormSubmitHandle()"
      >{{ $t('confirm') }}</el-button>
    </template> -->

    <el-dialog
      :visible.sync="UpdateVisible"
      :modal='false'
      title="修改商圈"
      width='20%'
    >
      <el-form
        :model="UpdateForm"
        ref="UpdateForm"
        label-width="50px"
        label-position='top'
      >
        <el-form-item
          label="商圈名称"
          prop="name"
        >
          <el-input
            v-model="UpdateForm.name"
            placeholder="商圈名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <template slot="footer">
        <el-button @click="UpdateVisible = false">取消</el-button>
        <el-button
          type="primary"
          @click="UpdateFormFun"
        >修改</el-button>
      </template>
    </el-dialog>
  </el-dialog>
</template>

<script>
import debounce from "lodash/debounce";
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: "",
        name: "",
        districtCode: "",
        latitude: "",
        tenantCode: "",
        creator: "",
        createDate: "",
        updater: "",
        updateDate: ""
      },
      inputVisible: false,
      inputValue: "",
      UpdateVisible: false,
      UpdateForm: {
        districtCode: "",
        name: ""
      }
    };
  },
  computed: {
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        } else {
          this.UpdateForm = {
            districtCode: "",
            name: ""
          };
          this.dataForm.list=[];
        }
      });
    },
    // 获取信息
    getInfo() {
      this.$http
        .get(`/dictionary/business/businessList/`, {
          params: {
            districtCode: this.dataForm.id
          }
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            list: res.data
          };
        })
        .catch(() => {});
    },

    //删除站点
    handleClose(tag) {
      let arr = [];
      arr.push(tag.id);
      this.$http
        .delete(`/dictionary/business`, {
          data: arr
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.getInfo();
          this.TagList.splice(this.TagList.indexOf(tag), 1);
        })
        .catch(() => {});
    },

    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },

    //点击
    handleClick(item) {
      this.UpdateVisible = true;
      this.UpdateForm = item;
    },

    //修改站点
    UpdateFormFun() {
      let _this = this;
      this.$http
        .put(`/dictionary/business`, this.UpdateForm)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$message.success("更改成功");
          this.UpdateVisible = false;
        })
        .catch(() => {});
    },

    //添加商圈
    handleInputConfirm() {
      let inputValue = this.inputValue;
      let _this = this;
      if (inputValue) {
        this.$http
          .post(`/dictionary/business`, {
            districtCode: _this.dataForm.id?_this.dataForm.id:_this.dataForm.districtCode,
            name: inputValue
          })
          .then(({ data: res }) => {
            if (res.code !== 0) {
              return this.$message.error(res.msg);
            }
            this.getInfo();
            this.$message.success("添加站点成功");
            this.$emit("refreshDataList");
          })
          .catch(() => {});
      }
      this.inputVisible = false;
      this.inputValue = "";
    },

    // 表单提交
    dataFormSubmitHandle: debounce(
      function() {
        this.$refs["dataForm"].validate(valid => {
          if (!valid) {
            return false;
          }
          this.$http[!this.dataForm.id ? "post" : "put"](
            "/dictionary/business/",
            this.dataForm
          )
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            })
            .catch(() => {});
        });
      },
      1000,
      { leading: true, trailing: false }
    )
  }
};
</script>
